package com.bw.mallshop.mvvm.viewmodel;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.bw.common.bus.LiveDataBus;
import com.bw.common.entity.BaseEntity;
import com.bw.common.field.ActionEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.mallshop.adapter.SkillAdapter;
import com.bw.mallshop.entity.SKillEntity;
import com.bw.mallshop.entity.SecKillEntity;
import com.bw.mallshop.field.TimeAction;
import com.bw.mallshop.mvvm.model.SKillListModel;
import com.bw.mallshop.mvvm.view.SkillValueActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SkillListViewModel extends BaseViewModel<SKillListModel> implements OnItemClickListener,
        OnItemChildClickListener {

    public SkillAdapter adapter;
    public ObservableField<String>titleStr=new ObservableField<>();
    private LiveDataBus<TimeAction>nowData = new LiveDataBus<>();
    public int type = 0;

    public LiveDataBus<TimeAction> getNowData() {
        return nowData;
    }

    @Override
    protected void initData() {
        adapter = new SkillAdapter();
        adapter.setOnItemChildClickListener(this);
        adapter.setOnItemClickListener(this);
        m.requestSkillList().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected void result(BaseEntity entity) {
        //区分秒杀列表以及秒杀
        if (entity instanceof SecKillEntity){
            //秒杀列表返回
            SecKillEntity secKillEntity = (SecKillEntity) entity;
            if (secKillEntity.data == null)
                return;
            if (secKillEntity.data.size() == 0)
                return;
            //服务器unix时间戳单位为S Android中直接获取时间戳单位为ms
            //在使用时间上传服务器时需要除1000，在使用服务器时间时需要×1000

            long start = secKillEntity.data.get(0).starttime*1000;
            long end = secKillEntity.data.get(0).endtime*1000;
            //时间戳转化获取开始的小时以及结束的小时
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(start);
            int startTime = calendar.get(Calendar.HOUR_OF_DAY);
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTimeInMillis(end);
            int endTime = endCalendar.get(Calendar.HOUR_OF_DAY);
            titleStr.set(""+startTime+"点到"+endTime+"点场");
            long now = secKillEntity.data.get(0).nowtime;
            //传递数据到skilllistactivity
            TimeAction action = new TimeAction();
            action.action = ActionEntity.SETTIME;
            action.time = now;
            nowData.setValue(action);
            adapter.setNewInstance(secKillEntity.data);
        }else if (entity instanceof SKillEntity){
            //秒杀

        }
    }

    public void finish(){
        finishPage();
    }

    SecKillEntity.Value bean;
    public void requestSkill(long time){
        //发起秒杀操作
        Map<String,Object>map = new HashMap<>();
        map.put("goodsCount",1);
        map.put("goodsDesc",bean.goods_desc);
        map.put("goodsIcon",bean.goods_default_icon);
        map.put("goodsId",bean.id);
        map.put("goodsPrice",Float.valueOf(TextUtils.isEmpty(bean.goods_default_price)
                ?"0":bean.goods_default_price)*1);
        map.put("goodsSku",bean.goods_default_sku);
        map.put("time",time);
        map.put("userId",1);
        m.requestSKill(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    /**
     * {
     *   "goodsCount": 0,
     *   "goodsDesc": "string",
     *   "goodsIcon": "string",
     *   "goodsId": 0,
     *   "goodsPrice": "string",
     *   "goodsSku": "string",
     *   "time": 0,
     *   "userId": 0
     * }
     * */
    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        bean = this.adapter.getItem(position);//获取当前点击数据
        TimeAction action = new TimeAction();
        action.action = ActionEntity.GETTIME;
        nowData.setValue(action);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        //跳转到秒杀详情界面
        bean = this.adapter.getItem(position);//获取当前点击数据
        Bundle bundle = new Bundle();
        bundle.putSerializable("skillBean",bean);//bundle传递数据数不能大于1mb
        startPage(SkillValueActivity.class,bundle);
    }
}
